<?php
class SocketShell extends AppShell
{
	public function main()
    {
        $address = "192.168.0.52"; //
        $port = 50000; //

        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
        if ($socket === false) {
            pr("socket_create() fail : " . socket_strerror(socket_last_error()));
        } else {
            pr("socket success.");
        }

        pr("IP $address and Port $port connecting...");

        $result = socket_connect($socket, $address, $port);
        if ($result === false) {
            pr("socket_connect() fail : ($result) " . socket_strerror(socket_last_error($socket)));
        } else {
            pr("socket connect success : $address");
        }

        $i = '{"jsonrpc":"2.0", "method":"system.notice", "message":"점검합니다."}';
        //$i = '{"jsonrpc":"2.0", "method":"op.command", "params":{"exp":200}}';

        $packet = pack('n', 2012).pack('n',strlen($i)+2).pack('n',strlen($i)).$i;
        socket_write($socket, $packet, strlen($i)+6);

        //$input = socket_read($socket, 1024) or die("Could not read from Socket\n");
        //pr($input);

        socket_close($socket);
    }

    public function testsocket()
    {
        $host    = "192.168.0.52";
        $port    = 50000;

        $message = "hi~1";

        $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket\n");
        socket_connect($socket, $host, $port);
        socket_write($socket, $message, strlen($message)) or die("Could not send data to server\n");

        socket_close($socket);
    }
}

